﻿import QtQuick 2.12
import QtQuick.Controls 2.12
import "../components"


// 复位弹窗
//
CustomPopupBox {

    id: root

    title: ""

    height: 480*dp; width: 440*dp

//    Column{

//        Text{
//            text: gStateReset.resetState_practice
//        }
//        Text{
//            text: gStateReset.resetState_shifting
//        }
//        Text{
//            text: gStateReset.resetState_purify
//        }
//        Text{
//            text: gStateReset.resetState_syringe1
//        }
//        Text{
//            text: gStateReset.resetState_syringe2
//        }
//        Text{
//            text: gStateReset.resetState_syringe3
//        }
//        Text{
//            text: gStateReset.resetState_syringe4
//        }
//        Text{
//            text: gStateReset.resetState_syringe5
//        }
//        Text{
//            text: gStateReset.resetState_syringe6
//        }
//    }


    property var partList: ["放靶/转靶模块", "打靶模块", "纯化模块", " 注射器1 ", " 注射器2 ", " 注射器3 ", " 注射器4 ", " 注射器5 ", " 注射器6 ", " 转盘 "]
    property var finishList: [false, false, false, false, false, false, false, false, false, false, false, false, false, false]

    function refreshFinishList(){
        // 大模块
        finishList[0] = gStateReset.resetState_shifting
        finishList[1] = gStateReset.resetState_practice
        finishList[2] = gStateReset.resetState_purify
        // 小模块
        finishList[3] = gStateReset.resetState_syringe1
        finishList[4] = gStateReset.resetState_syringe2
        finishList[5] = gStateReset.resetState_syringe3
        finishList[6] = gStateReset.resetState_syringe4
        finishList[7] = gStateReset.resetState_syringe5
        finishList[8] = gStateReset.resetState_syringe6
        finishList[9] = gStateReset.resetState_wheel
        finishListChanged()
    }


    property int infoState: 0       // 0:正在等待设备响应 1:正在复位 2复位完成

    Timer{
        id: timer_timeout0      // 设备没有返回01的超时
        interval: config.return01Timeout
        onTriggered: {
            infoState = 3
        }
    }
    Timer{
        id: timer_timeout1      // 设备没有返回02的超时
        interval: config.return02Timeout_short
        onTriggered: {
            infoState = 4
        }
    }

    onVisibleChanged: {
        if(visible){
            refreshFinishList()
            if(gExpStatus === 3){
                if(finishList[2] === -1){       // 判断是否需要复位注射器来调整等待时间
                    timer_timeout1.interval = config.return02Timeout_short
                }else{
                    timer_timeout1.interval = config.return02Timeout_long
                }
                timer_timeout1.start()
                timer_timeout0.stop()
                infoState = 1
            }else{
                timer_timeout0.start()
            }
        }else{
            infoState = 0
            gStateReset.clearResetState()
        }
    }
    Connections{
        target: mainwindow
        onGExpIsExecOperateChanged: {
            if(gExpIsExecOperate === 1 && root.visible){       // 收到开始复位的信号
                infoState = 1
                timer_timeout0.stop()
                if(finishList[2] === -1){       // 判断是否需要复位注射器来调整等待时间
                    timer_timeout1.interval = config.return02Timeout_short
                }else{
                    timer_timeout1.interval = config.return02Timeout_long
                }
                timer_timeout1.start()
            }else if(gExpIsExecOperate === 0 && (infoState === 1||infoState===4)){  // 收到复位完成的信号
                infoState = 2
                timer_timeout1.stop()
            }
        }
    }

    Timer{
        running: root.visible
        repeat: true
        interval: 500
        onTriggered: {
            refreshFinishList()
        }
    }

    contentItem: Item{

        Text {
            text: {
                if(infoState === 0)         return "正在等待设备响应"
                else if(infoState === 1)    return "设备复位中，请耐心等待"
                else if(infoState === 2)    return "设备复位完成"
                else if(infoState === 3)    return "设备响应超时"
                else if(infoState === 4)    return "设备复位超时"
            }
            font.bold: true
            font.pixelSize: config.fontsize_1
            x: 20*dp; y: -10*dp
        }

        ListView{
            visible: infoState !== 0 && infoState !== 3
            x: 20*dp; y: 32*dp
            height: parent.height - y - 80*dp
            width: parent.width - 2*x
            clip: true
            model: partList.length
            ScrollBar.vertical: ScrollBar{ }
            delegate: Rectangle {
                visible: {

                    if(finishList[index] === -1)  return false     // 控制某个模块状态是否显示
                    // 前三个模块的时候第四个纯化不显示
                    if(gAppModule === 1){
                        if(index > 1){
                            return false
                        }
                    }
                    // 纯化时前三个模块不显示
                    if(gAppModule === 2){
                        if(index <= 1){
                            return false
                        }
                    }
                    // gCollectorContainer=0时才显示转盘
                    if(gCollectContainer !== 0 && index === 9){
                        return false
                    }
                    return true
                }
                height: visible ? 44*dp : 0
                width: parent.width
                color: partList[index][0]===" " ? Qt.rgba(243/255, 246/255, 1) : "white"        // 前面带空格的是子模块
                Text {      // 零件文字
                    text: qsTr(partList[index])
                    font.pixelSize: config.fontsize * (partList[index][0]===" " ? 0.9 : 1)
                    anchors.centerIn: parent
                }Item{      // 转圈的动画
                    height: 40*dp; width: 40*dp
                    anchors.verticalCenter: parent.verticalCenter
                    x: parent.width - width
                    CustomAnimatedImage{
                        id: gif_resetting
                        source: "qrc:/picture/popupResetting/resetting.gif"
                        anchors.centerIn: parent
                        playing: visible
//                        visible: finishList[index] === 1 && infoState !== 4
                        visible: (finishList[index] === 1 && infoState !== 4) || (finishList[index] === 1 && infoState === 4)       // 取消了超时提示，把error的也加到这儿来了
                        height: 20*dp; width: 20*dp
                    }CustomImage{
                        source: "qrc:/picture/popupResetting/finish.png"
                        height: 20*dp; width: 20*dp
                        visible: finishList[index] === 0
                        anchors.centerIn: parent
                    }CustomImage{
                        source: "qrc:/picture/popupResetting/error.png"
                        height: 20*dp; width: 20*dp
//                        visible: finishList[index] === 1 && infoState === 4
                        visible: false
                        anchors.centerIn: parent
                    }
                }
            }
        }


        CustomAnimatedImage{
            id: gif_resetting_big
            source: "qrc:/picture/loading.gif"
            anchors.verticalCenterOffset: -10*dp
            anchors.centerIn: parent
            visible: infoState === 0
            playing: visible
            speed: 2
            height: 144*dp; width: 144*dp
        }Image {
            source: "qrc:/picture/popupResetting/timeout0.png"
            visible: infoState===3
            anchors.centerIn: parent
            anchors.verticalCenterOffset: -10*dp
            height: 196*dp; width: 196*dp
        }

        CustomButton{
            id: button_close
            y: parent.height - 60*dp
            visible: infoState === 2 || infoState === 3 || infoState === 4
            defaultMode2: true
            title: "关闭"
            anchors.horizontalCenter: parent.horizontalCenter
            onClicked: {
                close()
                gExpIsExecOperate = 0
            }
        }
        CustomButton{
            id: button_forceClose
            y: parent.height - 60*dp
            visible: infoState < 2
            defaultMode2: true
            title: "关闭"
            anchors.horizontalCenter: parent.horizontalCenter
            onClicked: {
                box_closeWarning.show()
            }
        }
    }

    onInfoStateChanged: {           // 状态切换时，强制关闭弹窗关闭，通知用户执行完成
        console.log("复位的InfoState改变为", infoState)
        if(infoState >= 2){
            box_closeWarning.close()
        }
    }

    CustomPopupBoxWarning{
        id: box_closeWarning
        box_text: "当前指令未执行完成，关闭弹窗后进行其它操作可能会导致设备异常。确认关闭吗？"
        onAccepted: {
            gExpIsExecOperate = 0
            timer_timeout0.stop()
            timer_timeout1.stop()
            root.close()
        }
    }
}
